//**************************************************
// Description: extract UDP field from N*128-bit data 
//**************************************************
// note: do not execute CRC check !

//**************************************************
// include files
//**************************************************
`include "protocol_define.v"

module UDP_deal #(
    parameter IN_DATA_WIDTH     = 128 ,
    parameter IN_MOD_WIDTH      = 4  
        )(
    input                           clk ,
    input                           rst_n ,
    // pre-module(unpack) interface
    output                          UDP_rdy ,
    input   [IN_DATA_WIDTH-1:0]     IPv4_payload ,
    input   [IN_MOD_WIDTH-1:0]      IPv4_mod ,
    input                           IPv4_sav ,
    input                           IPv4_val ,
    input                           IPv4_sop ,
    input                           IPv4_eop ,
    // "NETWORK" layer extracted field
    input   [31:0]                  IPv4_dst_IP ,
    input   [31:0]                  IPv4_src_IP ,
    // "DATA LINK" layer extracted field
    input   [47:0]                  DATALINK_src_mac ,
    input   [47:0]                  DATALINK_dst_mac ,
    // post-module(dedicated protocol deal) interface
    input                           PROTOCOL_check_rdy ,
    output  [IN_DATA_WIDTH-1:0]     PROTOCOL_check_data ,
    output  [IN_MOD_WIDTH-1:0]      PROTOCOL_check_mod ,
    output                          PROTOCOL_check_sav ,
    output                          PROTOCOL_check_val ,
    output                          PROTOCOL_check_sop ,
    output                          PROTOCOL_check_eop ,
    // "FIFO" store signal
    output                          fifo_wr_en ,
    output  [47:0]                  DATALINK_dst_mac_fifo ,
    output  [47:0]                  DATALINK_src_mac_fifo ,
    output  [31:0]                  NETWORK_src_IP_fifo ,
    output  [31:0]                  NETWORK_dst_IP_fifo ,
    output  [15:0]                  TRANSPORT_src_port_fifo ,
    output  [15:0]                  TRANSPORT_dst_port_fifo ,
    // NP config register
    input   [47:0]                  NP_MAC_address ,
    input   [31:0]                  NP_IP_address ,
    input   [15:0]                  NP_port_address
) ;

    // SIM only
    // wire field_enable ;
    // reg CRC_check_result ;
    // always @(posedge clk or negedge rst_n) begin
    //     if( !rst_n )
    //         CRC_check_result <= 1'b0 ;
    //     else if( field_enable )
    //         CRC_check_result <= 1'b1 ;
    //     else 
    //         CRC_check_result <= 1'b0 ;
    // end

    // interface between "UDP deal" module and "field check" module
    wire                            field_enable ;
    wire                            field_check_result ;
    wire                            field_check_success ;

    wire    [47:0]                  DATALINK_dst_mac_extracted ;
    wire    [47:0]                  DATALINK_src_mac_extracted ;
    wire    [31:0]                  NETWORK_src_IP_extracted ;
    wire    [31:0]                  NETWORK_dst_IP_extracted ;
    wire    [15:0]                  TRANSPORT_src_port_extracted ;
    wire    [15:0]                  TRANSPORT_dst_port_extracted ;

//**************************************************
// UDP deal module instance
//**************************************************
    UDP_extract #(
        .IN_DATA_WIDTH(IN_DATA_WIDTH) ,
        .IN_MOD_WIDTH(IN_MOD_WIDTH)
        )
    inst_UDP_extract(
        .clk                            (clk                            ) ,
        .rst_n                          (rst_n                          ) ,
        // pre-module interface
        .UDP_rdy                        (UDP_rdy                        ) ,
        .IPv4_payload                   (IPv4_payload                   ) ,
        .IPv4_mod                       (IPv4_mod                       ) ,
        .IPv4_sav                       (IPv4_sav                       ) ,
        .IPv4_val                       (IPv4_val                       ) ,
        .IPv4_sop                       (IPv4_sop                       ) ,
        .IPv4_eop                       (IPv4_eop                       ) ,
        // extracted field
        .IPv4_src_IP                    (IPv4_src_IP                    ) ,
        .IPv4_dst_IP                    (IPv4_dst_IP                    ) ,
        .DATALINK_src_mac               (DATALINK_src_mac               ) ,
        .DATALINK_dst_mac               (DATALINK_dst_mac               ) ,
        // post-module interface
        .UDP_checked_rdy                (PROTOCOL_check_rdy             ) ,
        .UDP_checked_data               (PROTOCOL_check_data            ) ,
        .UDP_checked_mod                (PROTOCOL_check_mod             ) ,
        .UDP_checked_sav                (PROTOCOL_check_sav             ) ,
        .UDP_checked_val                (PROTOCOL_check_val             ) ,
        .UDP_checked_sop                (PROTOCOL_check_sop             ) ,
        .UDP_checked_eop                (PROTOCOL_check_eop             ) ,
        // check and store
        .field_enable                   (field_enable                   ) ,
        .DATALINK_dst_mac_extracted     (DATALINK_dst_mac_extracted     ) ,
        .DATALINK_src_mac_extracted     (DATALINK_src_mac_extracted     ) ,
        .NETWORK_src_IP_extracted       (NETWORK_src_IP_extracted       ) ,
        .NETWORK_dst_IP_extracted       (NETWORK_dst_IP_extracted       ) ,
        .TRANSPORT_src_port_extracted   (TRANSPORT_src_port_extracted   ) ,
        .TRANSPORT_dst_port_extracted   (TRANSPORT_dst_port_extracted   ) ,
        .check_result                   (field_check_result             ) ,
        .check_sucess                   (field_check_success            )
    ) ;



//**************************************************
// field check module instance
//**************************************************
    UDP_field_check inst_UDP_field_check (
        .clk                            (clk                            ) ,
        .rst_n                          (rst_n                          ) ,
        // UDP deal module
        .field_enable                   (field_enable                   ) ,
        .DATALINK_dst_mac_extracted     (DATALINK_dst_mac_extracted     ) ,
        .DATALINK_src_mac_extracted     (DATALINK_src_mac_extracted     ) ,
        .NETWORK_src_IP_extracted       (NETWORK_src_IP_extracted       ) ,
        .NETWORK_dst_IP_extracted       (NETWORK_dst_IP_extracted       ) ,
        .TRANSPORT_src_port_extracted   (TRANSPORT_src_port_extracted   ) ,
        .TRANSPORT_dst_port_extracted   (TRANSPORT_dst_port_extracted   ) ,
        .field_check_result             (field_check_result             ) ,
        .field_check_success            (field_check_success            ) ,
        // NP register config
        .NP_MAC_address                 (NP_MAC_address                 ) ,
        .NP_IP_address                  (NP_IP_address                  ) ,
        .NP_port_address                (NP_port_address                ) ,
        // FIFO interface
        .fifo_wr_en                     (fifo_wr_en                     ) ,
        .DATALINK_dst_mac_fifo          (DATALINK_dst_mac_fifo          ) ,
        .DATALINK_src_mac_fifo          (DATALINK_src_mac_fifo          ) ,
        .NETWORK_src_IP_fifo            (NETWORK_src_IP_fifo            ) ,
        .NETWORK_dst_IP_fifo            (NETWORK_dst_IP_fifo            ) ,
        .TRANSPORT_src_port_fifo        (TRANSPORT_src_port_fifo        ) ,
        .TRANSPORT_dst_port_fifo        (TRANSPORT_dst_port_fifo        )
    ) ;

endmodule
